package com.example.module_motocircle.motocircledetail

import android.graphics.Color
import android.text.SpannableStringBuilder
import android.text.method.LinkMovementMethod
import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.example.lib_common.dp
import com.example.lib_common.getAliOSSVideoImage
import com.example.lib_image.GlideApp
import com.example.lib_image.Info
import com.example.module_motocircle.databinding.LayoutPostDetailsItemBinding
import com.example.module_motocircle.model.DetailModel
import com.example.module_motocircle.model.detailTopic
import com.google.android.exoplayer2.util.MimeTypes

/**
 *@author Wcj
 *@description
 *@date 2022/7/4 14:56
 */
class PostDetailsViewHolder(
    itemView: View,
    private val onImageClick: ((Int, Int, List<String>, List<Info>) -> Unit)? = null,
    private val onUserClick: ((String, Boolean) -> Unit)? = null,
    private val onFollowClick: ((post: DetailModel) -> Unit)? = null,
) : RecyclerView.ViewHolder(itemView) {
    private val mBinding: LayoutPostDetailsItemBinding by lazy {
        DataBindingUtil.getBinding(this.itemView)!!
    }
    lateinit var postModel: DetailModel

    var isClickSpan: Boolean = false


    fun initView(post: DetailModel, position: Int) {

        mBinding.apply {
            handlePicOrVideo(post, position)
            postModel = post
            //处理帖子中携带的话题
            if (!post.topics.isNullOrEmpty())
                handlePostTopic(post, post.topics)
        }
    }

    private fun LayoutPostDetailsItemBinding.handlePicOrVideo(post: DetailModel, position: Int) {
        post.image ?: return
        if (post.image.size == 1 && MimeTypes.isVideo(post.image[0].contentType)) {
            //视频
            nineGridView.visibility = View.GONE
            lVideo.visibility = View.VISIBLE
            lVideo.setOnClickListener {
//                VideoPlayerActivity.jump(post.image[0].url)
            }
            val l = lVideo.layoutParams
            val video = post.image.firstOrNull() ?: return
            val videoHeight = video.imageHeight
            val videoWidth = video.imageWidth
            if (videoHeight > videoWidth) {
                l.width = 233.dp
                l.height = 311.dp
            } else {
                l.width = 343.dp
                l.height = 180.dp
            }
            lVideo.layoutParams = l
            GlideApp.with(this.root).load(getAliOSSVideoImage(post.image[0].url!!))
                .into(ivVideo)
        } else {
            nineGridView.visibility = View.VISIBLE
            lVideo.visibility = View.GONE
            //图片
            nineGridView.setPicsList(post.image!!)
//            onImageClick?.let {
//                nineGridView.setOnItemClickListener(onImageClick)
//            }
            nineGridView.initView()
        }

    }

    private fun handlePostTopic(
        post: DetailModel,
        topics: List<detailTopic>
    ) {
        val content = post.requireContent()
        val spannableStringBuilder = SpannableStringBuilder(content)
        //保存topic的起始位置
        val topicIndexMap = mutableMapOf<String, MutableList<Int>>()
        //保存topicId
        val topicIdMap = mutableMapOf<String, String>()
        //记录截取起始位置
        topics.forEach {
            //转换为#Topic#
            val topicName = String.format("#%s#", it.topicName)
            var temp = content
            var p = 0
            while (true) {
                val start = temp.indexOf(topicName)
                if (start > -1) {
                    if (topicIndexMap[topicName] == null) {
                        topicIndexMap[topicName] = mutableListOf()
                    }
                    topicIndexMap[topicName]!!.add(start + p)
                    topicIdMap[topicName] = it.topicId.toString()
                } else {
                    break
                }
                p += start + topicName.length
                temp = content.substring(p)
            }
        }
        //添加点击事件
//        topicIndexMap.forEach { map ->
//            map.value.forEach {
//                spannableStringBuilder.setSpan(TopicSpan(map.key, ""), it, it + map.key.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
//                spannableStringBuilder.setSpan(
//                    object : ClickableSpan() {
//                        override fun onClick(p0: View) {
//                            isClickSpan = true
//                            (p0 as TextView).highlightColor = Color.TRANSPARENT
//                            TopicActivity.jump(topicIdMap[map.key] ?: "", map.key)
//                        }
//
//                        override fun updateDrawState(ds: TextPaint) {
//                            ds.color = ContextCompat.getColor(itemView.context, R.color.topic)
//                            ds.isUnderlineText = false
//                        }
//                    },
//                    it, it + map.key.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
//                )
//            }
//        }
        mBinding.tvPostContent.movementMethod = LinkMovementMethod.getInstance()
        mBinding.tvPostContent.text = spannableStringBuilder
        mBinding.tvPostContent.highlightColor = Color.TRANSPARENT

    }

//    fun clickUser() {
//        if (postModel.isOfficial()) return
//        onUserClick?.invoke(postModel.createBy, postModel.isOfficial())
//    }
//
//    fun clickFollow() {
//        onFollowClick?.invoke(postModel)
//    }
}
